#!/bin/sh

TEST_DIR=''

cleanup () {
  case "${TEST_DIR}" in
    /*) rm -rf "${TEST_DIR}" ;;
  esac
  unset -f nvm nvm_npm reset_records
  unset MOCK_USE_STATUS MOCK_NPM_OUTPUT NPMLIST VERSION INSTALLS LINKS
}

die () { echo "$@" ; cleanup ; exit 1; }

: nvm.sh
\. ../../../nvm.sh

\. ../../common.sh

TEST_DIR="$(mktemp -d)" || die 'could not create temp dir'

nvm() {
  printf '%s\n' "$@" > "${TEST_DIR}/use_args"
  echo 'use stdout'
  echo 'use stderr' >&2
  return "${MOCK_USE_STATUS}"
}

nvm_npm() {
  printf '%s\n' "$@" > "${TEST_DIR}/npm_args"
  echo 'npm stderr' >&2
  printf '%s\n' "${MOCK_NPM_OUTPUT}"
}

reset_records() {
  rm -f "${TEST_DIR}/use_args" "${TEST_DIR}/npm_args"
}

MOCK_USE_STATUS=0
MOCK_NPM_OUTPUT='/fake/lib
├── @scope/pkg@1.2.3
├── corepack@0.20.0
├── corepack-helper@1.0.0
├── linked@0.0.1 -> /home/u/my src/linked
├── lodash@4.17.21 extraneous
├── npm@10.0.0
├── npm-check@6.0.1
├── not-corepack@3.0.0
├── not-npm@4.0.0
├── UNMET PEER DEPENDENCY peer@1.0.0
└── second@2.0.0 -> ./relative/second'

reset_records
try nvm_npm_global_modules 'lts/*'
EXPECTED='@scope/pkg@1.2.3 corepack-helper@1.0.0 lodash@4.17.21 npm-check@6.0.1 not-corepack@3.0.0 not-npm@4.0.0 //// /home/u/my src/linked
./relative/second'
[ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "packages and links: got >${CAPTURED_STDOUT}<, expected >${EXPECTED}<"
[ "_${CAPTURED_EXIT_CODE}" = '_0' ] || die "packages and links: exit code >${CAPTURED_EXIT_CODE}<, expected >0<"

USE_ARGS="$(cat "${TEST_DIR}/use_args")"
EXPECTED='use
lts/*'
[ "_${USE_ARGS}" = "_${EXPECTED}" ] || die "nvm was called with >${USE_ARGS}<, expected >${EXPECTED}<"

NPM_ARGS="$(cat "${TEST_DIR}/npm_args")"
EXPECTED='list
-g
--depth=0'
[ "_${NPM_ARGS}" = "_${EXPECTED}" ] || die "nvm_npm was called with >${NPM_ARGS}<, expected >${EXPECTED}<"

try_err nvm_npm_global_modules v1.2.3
[ "_${CAPTURED_STDERR}" = '_use stderr' ] || die "stderr: got >${CAPTURED_STDERR}<, expected >use stderr<"

NPMLIST=sentinel
VERSION=sentinel
INSTALLS=sentinel
LINKS=sentinel
nvm_npm_global_modules v1.2.3 >/dev/null 2>&1
[ "_${NPMLIST}" = '_sentinel' ] || die "NPMLIST leaked: got >${NPMLIST}<, expected >sentinel<"
[ "_${VERSION}" = '_sentinel' ] || die "VERSION leaked: got >${VERSION}<, expected >sentinel<"
[ "_${INSTALLS}" = '_sentinel' ] || die "INSTALLS leaked: got >${INSTALLS}<, expected >sentinel<"
[ "_${LINKS}" = '_sentinel' ] || die "LINKS leaked: got >${LINKS}<, expected >sentinel<"
unset NPMLIST VERSION INSTALLS LINKS

reset_records
try nvm_npm_global_modules '*'
USE_ARGS="$(cat "${TEST_DIR}/use_args")"
EXPECTED='use
*'
[ "_${USE_ARGS}" = "_${EXPECTED}" ] || die "nvm was called with >${USE_ARGS}<, expected >${EXPECTED}<"

MOCK_NPM_OUTPUT='/fake/lib
└── (empty)'
try nvm_npm_global_modules v1.2.3
[ "_${CAPTURED_STDOUT}" = '_ //// ' ] || die "empty list: got >${CAPTURED_STDOUT}<, expected > //// <"

MOCK_NPM_OUTPUT='/fake/lib
├── only-link@1.0.0 -> /a/b
└── npm@10.0.0'
try nvm_npm_global_modules v1.2.3
[ "_${CAPTURED_STDOUT}" = '_ //// /a/b' ] || die "links only: got >${CAPTURED_STDOUT}<, expected > //// /a/b<"

MOCK_NPM_OUTPUT='/fake/lib
└── only-install@1.0.0'
try nvm_npm_global_modules v1.2.3
[ "_${CAPTURED_STDOUT}" = '_only-install@1.0.0 //// ' ] || die "installs only: got >${CAPTURED_STDOUT}<, expected >only-install@1.0.0 //// <"

reset_records
MOCK_USE_STATUS=3
MOCK_NPM_OUTPUT='/fake/lib
└── should-not-appear@1.0.0'
try nvm_npm_global_modules v9.9.9
[ "_${CAPTURED_STDOUT}" = '_ //// ' ] || die "failed nvm use: got >${CAPTURED_STDOUT}<, expected > //// <"
[ -f "${TEST_DIR}/npm_args" ] && die 'failed nvm use: nvm_npm was called'

cleanup
